package atech.ace.mapping;

import java.io.IOException;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;

import atech.ace.mapping.persistence.entities.HumanResource;
import atech.ace.mapping.persistence.entities.Project;

/**
 * Servlet implementation class DeleteResource
 */
@WebServlet("/DeleteResource")
public class DeleteResource extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@PersistenceContext(name = "jdbc/ACE_ProjectMapping")
	EntityManager em;

	@Resource
	UserTransaction utx;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		boolean succeeded = true;
		String stringId = request.getParameter("id");
		if (stringId == null)
			succeeded = false;
		else {
			try {
				utx.begin();
				HumanResource resource = em.find(HumanResource.class, stringId);
				em.remove(resource);
				utx.commit();
			} catch (Exception e) {
				succeeded = false;
				e.printStackTrace();
				try {
					utx.rollback();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
		if (succeeded) {
			response.sendRedirect("Create");
		}
		else {
			response.sendRedirect("Home");
		}
	}

}
